package cn.com.jinhao.provider.business.service.impl;

import cn.com.jinhao.hotline.commons.base.constant.SystemConstant;
import cn.com.jinhao.hotline.commons.exception.BusinessErrorException;
import cn.com.jinhao.hotline.commons.vo.system.SysDictResponseVO;
import cn.com.jinhao.provider.business.entity.SysDict;
import cn.com.jinhao.provider.business.mapper.SysDictMapper;
import cn.com.jinhao.provider.business.service.SysDictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class SysDictServiceImpl implements SysDictService {
    @Resource
    SysDictMapper sysDictMapper;

    public Map<String, Object> querySysDict(String paid, String deptCode,int pageNo, int pageSize) {
        String parentCode = "";
        for (SystemConstant.SysDict e : SystemConstant.SysDict.values()) {
            if (e.getCode().equals(paid)) {
                parentCode = e.getName();
            }
        }
        parentCode = parentCode.equals("")?paid:parentCode;

        HashMap<String, Object> map = new HashMap<>(10);
        if (pageNo != 0 && pageSize != 0) {
            PageHelper.startPage(pageNo, pageSize);
            PageInfo<SysDictResponseVO> pageInfo = new PageInfo<>(sysDictMapper.queryByPaid(parentCode,deptCode));
            map.put("list", pageInfo);
            map.put("total", pageInfo.getTotal());
            map.put("pageSize", pageInfo.getPageSize());
            map.put("pageNo", pageInfo.getPageNum());
        } else {
            List<SysDictResponseVO> sysDicts = sysDictMapper.queryByPaid(parentCode,deptCode);
            map.put("list", sysDicts);
            map.put("total", sysDicts.size());
        }
        return map;
    }

    @Override
    public SysDict queryByCodeAndPaid(String code, String paid) {
        SysDict sysDict = new SysDict();
        sysDict.setCode(code);
        sysDict.setPaid(paid);

        return sysDictMapper.selectOne(sysDict);
    }

    @Override
    public void add(SysDict sysDict) throws Exception {
        String code = sysDictMapper.getMaxCode(sysDict.getPaid());
        sysDict.setCode(code);
        int result = sysDictMapper.insertSelective(sysDict);

        // 更新记录为0，且父编码不为空的情况下，抛出异常
        if (result == 0)  {
            Optional.ofNullable(sysDict.getPaid()).orElseThrow(() -> new BusinessErrorException("父编码不存在，请检查"));
        }
    }

    @Override
    public void delete(String code, String paid) throws Exception {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", code);
        if (StringUtils.isNotBlank(paid)) {
            criteria.andEqualTo("paid", paid);
        }

        int result = sysDictMapper.deleteByExample(example);

        if (result == 0 && StringUtils.isNotBlank(paid)) {
            throw new BusinessErrorException("父编码不存在，请检查");
        }
    }

    @Override
    public void update(SysDict sysDict) throws Exception {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", sysDict.getCode());
        if (Optional.ofNullable(sysDict.getPaid()).isPresent()) {
            criteria.andEqualTo("paid", sysDict.getPaid());
        }

        int result = sysDictMapper.updateByExampleSelective(sysDict, example);

        if (result == 0) {
            Optional.ofNullable(sysDict.getPaid()).orElseThrow(() -> new BusinessErrorException("父编码不存在，请检查"));
        }
    }

    @Override
    public void updateById(SysDict sysDict) {
        sysDictMapper.updateByPrimaryKeySelective(sysDict);
    }

    @Override
    public void delete(String id) {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        sysDictMapper.deleteByExample(example);
    }
}
